package com.hub.realtime.resource;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.realtime.resource.dto.input.JarManagementQueryParam;
import com.hub.realtime.resource.dto.input.UploadQueryParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hub.realtime.common.annotation.Log;
import com.hub.realtime.common.core.controller.BaseController;
import com.hub.realtime.common.core.domain.AjaxResult;
import com.hub.realtime.common.enums.BusinessType;
import com.hub.realtime.resource.domain.ResJarManagement;
import com.hub.realtime.resource.service.IResJarManagementService;
import com.hub.realtime.common.utils.poi.ExcelUtil;
import com.hub.realtime.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.hub.realtime.common.constant.Constants.JAR_TYPE_UDF;

/**
 * Jar包管理Controller
 *
 * @author wupeng
 * @date 2022-01-14
 */
@RestController
@RequestMapping("/resource/jarmanagement")
public class ResJarManagementController extends BaseController {
    @Autowired
    private IResJarManagementService resJarManagementService;


    /**
     * 查询Jar包管理列表
     */
    @PreAuthorize("@ss.hasPermi('resource:jarmanagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(JarManagementQueryParam param) {
        Page<ResJarManagement> page = resJarManagementService.list(param);
        return getDataTable(page.getRecords(), page.getTotal());
    }

    /**
     * 导出Jar包管理列表
     */
    @PreAuthorize("@ss.hasPermi('resource:jarmanagement:export')")
    @Log(title = "Jar包管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JarManagementQueryParam param) {
        List<ResJarManagement> list = resJarManagementService.list(param).getRecords();
        ExcelUtil<ResJarManagement> util = new ExcelUtil<ResJarManagement>(ResJarManagement.class);
        util.exportExcel(response, list, "Jar包管理数据");
    }

    /**
     * 获取Jar包管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:jarmanagement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(resJarManagementService.getBaseMapper().selectById(id));
    }

    /**
     * 新增Jar包管理
     */
    @PreAuthorize("@ss.hasPermi('resource:jarmanagement:add')")
    @Log(title = "Jar包管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResJarManagement resJarManagement) {
        return toAjax(resJarManagementService.saveInfo(resJarManagement));
    }

    /**
     * 修改Jar包管理
     */
    @PreAuthorize("@ss.hasPermi('resource:jarmanagement:edit')")
    @Log(title = "Jar包管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResJarManagement resJarManagement) {
        return toAjax(resJarManagementService.getBaseMapper().updateById(resJarManagement));
    }

    /**
     * 删除Jar包管理
     */
    @PreAuthorize("@ss.hasPermi('resource:jarmanagement:remove')")
    @Log(title = "Jar包管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(resJarManagementService.deleteInfos(ids));
    }

    @PostMapping(value = "/createUploadFile")
    public AjaxResult createUploadFile(@RequestParam("file") MultipartFile file,
                                       @RequestParam("applicationId") Long applicationId) {
        return AjaxResult.success("上传成功", resJarManagementService.createUploadFile(file, applicationId));
    }

    @PostMapping(value = "/removeHdfsTmp")
    public AjaxResult removeHdfsTmp(UploadQueryParam uploadQueryParam) {
        resJarManagementService.removeHdfsTmp(uploadQueryParam);
        return AjaxResult.success();
    }

    @GetMapping(value = "/getJarListByAppId/{applicationId}")
    public TableDataInfo getJarListByAppId(@PathVariable("applicationId") Long applicationId) {
        List<ResJarManagement> list = resJarManagementService.getJarListByAppId(applicationId);
        return getDataTable(list, null);
    }

    /**
     * 获取UDF列表
     */
    @GetMapping("/udfList")
    public AjaxResult getUdfList() {
        LambdaQueryWrapper<ResJarManagement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResJarManagement::getJarType, JAR_TYPE_UDF);
        List<ResJarManagement> list = resJarManagementService.getBaseMapper().selectList(queryWrapper);
        return AjaxResult.success(list);
    }


}
